package com.ug_project.views

import android.app.Activity
import android.graphics.Point
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.view.ViewGroup
import android.widget.EditText
import com.ug_project.Animation.errAnim
import com.ug_project.alert.alert

/**
 * Created by yeganaaa on 3/27/2017.
 */

val Activity.contentView : ViewGroup
get() = findView(android.R.id.content) as ViewGroup

fun Activity.clearEdit(edits: Array<EditText>) {
    edits.forEach {
        it.content = ""
    }
}

fun Activity.clearEdits() {
    (findViewById(android.R.id.content) as ViewGroup).childAll.filter { it is EditText }.forEach {
        (it as EditText).content = ""
    }
}

fun Activity.errAlert() {
    alert(getString(R.string.text_failed))
}

fun Activity.errAlert(msg: String) {
    alert(getString(R.string.text_failed), msg)
}

fun goEmptyEdits(it: android.widget.EditText) {
    it.requestFocus();it.error = it.hint;it.errAnim();it.showKeyBoard()
}

fun Activity.checkEdits(action: () -> Unit) {
    if (contentView.checkEmptyEditTexts(::goEmptyEdits)) {
        action()
    }
}

fun Activity.clearAllEdits() {
    contentView.childAll.filter { it is EditText }.forEach { (it as EditText).content = "" }
}

fun Activity.successToast() {
    showToast(getString(R.string.text_success))
}

val Activity.displayWidth: Int
    get() {
        var display = windowManager.defaultDisplay
        var point = Point()
        display.getSize(point)
        return point.x
    }
val Activity.displayHeight: Int
    get() {
        var display = windowManager.defaultDisplay
        var point = Point()
        display.getSize(point)
        return point.y
    }

fun Activity.requestPermiss(permissions: Array<String>, resultCode: Int = 0) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        ActivityCompat.requestPermissions(this, permissions, resultCode)
    }

}